package expressions.slogo;

import java.util.List;
import model.Angle;
import parsing.Token;

/**
 * Returns the angle to which the turtle must face to get to his destination.
 */
@Token(regex = "towards")
public class Towards extends TurtleCommand
{

    public Towards()
    {
        super(2);
    }

    @Override
    protected Double combineResults(List<Double> results, ITurtleContext context)
    {
        double xtarget = results.get(0);
        double ytarget = results.get(1);

        double xstart = context.getTurtle().getX();
        double ystart = context.getTurtle().getY();

        if (xstart == xtarget && ystart == ytarget)
            return 0D;

        double radians = Math.atan2(ytarget - ystart, xtarget - xstart);

        if (radians < 0)
            radians += 2 * Math.PI;

        return Angle.radiansToDegrees(radians);
    }

}
